import config from './config'
var request={};
var api=config.api;
request.post=function(method,data={}){
	var token = uni.getStorageSync('tokentp');
	var url=api+method;
	if(method.indexOf("http://")>-1||method.indexOf("https://")>-1){
		url=method;
	}
	
	var _defaultdata={};
	data=Object.assign(_defaultdata,data);
	return new Promise((resolve,reject)=>{
		console.log('请求全部参数:------------------------------------\r\n');
		console.log(url,JSON.stringify(data));
		uni.showLoading({
			mask:true
		})
		uni.request({
			url: url,
			method: 'post',
			header:{
				'content-type':'application/json; charset=utf-8',
				'xx-token':token
			},
			data: data,
			success: res => {
				uni.hideLoading()
				var data=res.data;
				console.log('返回结果:---------------------------------------\r\n');
				console.log(data);
				if(data.code==101||data.code==201){//token失效
					uni.removeStorageSync('tokentp');
					// #ifdef H5
					uni.setStorageSync("prepagetp",location.href)
					// #endif
				}
				if(data.code&&data.code!=200){
					uni.showToast({
						title:data.message,
						icon:'none'
					})
					return false;
				}
				
				resolve(data); 
			},
			fail: (err) => { 
				uni.showToast({
					title: '服务器错误!',
					icon:'none'
				});
			},
			complete: () => {}
		});
	})
}

request.ajax=function(url,data={}){
	var token = uni.getStorageSync('tokentp');
	var _defaultdata={};
	data=Object.assign(_defaultdata,data);
	return new Promise((resolve,reject)=>{
		console.log('请求全部参数:------------------------------------\r\n');
		console.log(url,JSON.stringify(data));
		uni.showLoading({
			mask:true
		})
		uni.request({
			url: url,
			method: 'post',
			header:{
				'content-type':'application/json; charset=utf-8',
				'xx-token':token
			},
			data: data,
			success: res => {
				uni.hideLoading()
				var data=res.data;
				console.log('返回结果:---------------------------------------\r\n');
				console.log(data);
				if(data.code!==0){
					uni.showToast({
						title:data.msg,
						icon:'none'
					})
					return false;
				}
				
				resolve(data); 
			},
			fail: (err) => { 
				uni.showToast({
					title: '服务器错误!',
					icon:'none'
				});
			},
			complete: () => {}
		});
	})
}
request.get=function(method,data={},showLoading=true){
	var token = uni.getStorageSync('tokentp'); 
	var url=api+method;
	var _defaultdata={};
	data=Object.assign(_defaultdata,data);
	return new Promise((resolve,reject)=>{
		console.log('请求全部参数:------------------------------------\r\n');
		console.log(url,JSON.stringify(data));
		if(showLoading){
			uni.showLoading({
				mask:true
			})
		}
	
		uni.request({
			url: url,
			method: 'get',
			header:{
				'content-type':'application/json; charset=utf-8',
				'xx-token':token
			},
			data: data,
			success: res => {
				uni.hideLoading()
				var data=res.data;
				console.log('返回结果:---------------------------------------\r\n');
				console.log(data);
				if(data.code==101||data.code==201){//token失效
					uni.setStorageSync("prepagetp",location.href)
					uni.removeStorageSync('tokentp');
				}
				
				if(data.code!=200){
					uni.showToast({
						title:data.message,
						icon:'none'
					})
					return false;
				}
				resolve(data); 
			},
			fail: (err) => { 
				uni.showToast({
					title: '服务器错误!',
					icon:"error"
				});
			},
			complete: () => {}
		});
	})
}

//微信公众号网页授权

request.getUseinfo=function(code=null,callback){
	console.log(code)
	let local = window.location.href;
	if(code==null) {
		window.location.href =`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${config.appid}&redirect_uri=${encodeURIComponent(local)}&response_type=code&scope=snsapi_base&state=1&connect_redirect=1#wechat_redirect`;
		return false;
	}
	//获取code
	request.get('wechat/getOpenId',{code:code,appid:config.appid},false).then(res=>{
		if(res.errcode){
			uni.showToast({
				title:res.result.errmsg
			})
		}else{
			uni.setStorageSync('tokentp', res.result.openid);
			callback&&callback(res.result.openid)
		}
	})
}

request.getUserList=function(code,callback,brand=0) {
	let list=[];
	uni.showLoading()
	request.get('wechat/queryCustomerByWx', {
		code: uni.getStorageSync("tokentp")
	}).then(res => {
		console.log(res)
		console.log(res.result)
		uni.hideLoading()
		if (res.result.length > 0) {
			res.result.forEach(item=>{
				if(item.meterType==6 || item.meterType==5){
					item.meterType="NB远传阀控表"
				}
				list.push(item)
			})
		}
		console.log(list)
		uni.setStorageSync("userListtp",list);
		callback&&callback(list);
	})
}


//判断是否微信登陆
  request.isWeiXin=function() {
         var ua = window.navigator.userAgent.toLowerCase();
         console.log(ua);//mozilla/5.0 (iphone; cpu iphone os 9_1 like mac os x) applewebkit/601.1.46 (khtml, like gecko)version/9.0 mobile/13b143 safari/601.1
         if (ua.match(/MicroMessenger/i) == 'micromessenger') {
             return true;
         } else {
             return false;
         }
    }
export default request
